using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;

namespace Easycmd
{
    public partial class DirDialog : Form
    {
        public DirDialog()
        {
            InitializeComponent();
            this.CancelButton = button2;
            this.StartPosition = FormStartPosition.CenterScreen;
        }
        public static string GetDriver()
        {
            string r = "";
            DirDialog ib = new DirDialog();
            ib.InitListView();
            ib.Fill();
            if (ib.lvDirList.Items.Count > 0)
            {
                ib.lvDirList.SelectedIndices.Add(0);
            }
            if (ib.ShowDialog() == DialogResult.OK)
            {
                if (ib.lvDirList.SelectedItems.Count>0)
                    r = ib.lvDirList.SelectedItems[0].Text;
            }
            return r;
        }
        private MruList mrulist = new MruList();
        public static string GetUsage()
        {
            string r = "";
            DirDialog ib = new DirDialog();
            ib.InitListView();
            ib.Fill(ib.mrulist.ListUsage());
            if (ib.lvDirList.Items.Count > 0)
            {
                ib.lvDirList.SelectedIndices.Add(0);
            }            
            if (ib.ShowDialog() == DialogResult.OK)
            {
                if (ib.lvDirList.SelectedItems.Count > 0)
                    r = ib.lvDirList.SelectedItems[0].SubItems[1].Text;
            }
            return r;
        }

        private void Fill(ArrayList dirs)
        {
            foreach (string a in dirs)
            {
                DirectoryInfo d = new DirectoryInfo(a);
                string name = "", type = "", size = "", datetime = "";
                name = d.Name;
                type = "<dir>";
                size = "";
                FillFile(lvDirList, a, type, size, datetime);
            }
        }

        private void Fill()
        {
             DriveInfo[] allDrives = DriveInfo.GetDrives();
             foreach (DriveInfo d in allDrives)
             {
                 string name = "", type = "", size = "", datetime = "";

                 name = d.Name;
                 type = d.DriveType.ToString();
                 if (d.IsReady)
                 {                 
                     size = d.AvailableFreeSpace.ToString() + "/" + d.TotalSize.ToString();
                 }
                 FillFile(lvDirList, name, type, size, datetime);
                 
             }
        }
        private void FillFile(ListView lv, string filename, string ext, string size, string datetime)
        {
            ListViewItem v = new ListViewItem();
            v.Text = filename;
            v.SubItems.Add(ext);
            v.SubItems.Add("");
            lv.Items.Add(v);
        }
        private void InitListView()
        {
            ListView lv = lvDirList;
            lv.View = View.Details;
            ColumnHeader ch ;      
            ch = new ColumnHeader();
            ch.Text = "File Name";
            ch.Width = 50;
            lv.Columns.Add(ch);
            ch = new ColumnHeader();
            ch = new ColumnHeader();
            ch.Text = "Size";
            lv.Columns.Add(ch);
            ch = new ColumnHeader();
            ch.Text = "DateTime";
            lv.Columns.Add(ch);
        }

        private void lvDrivers_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void lvDrivers_KeyUp(object sender, KeyEventArgs e)
        {
            //Text = e.KeyValue.ToString();
            if (e.KeyValue == 13) { this.DialogResult = DialogResult.OK; }
            if (e.KeyValue == 46) { Delete(); }
            
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            Delete();
        }

        private void Delete()
        {
            foreach (ListViewItem lvi in lvDirList.SelectedItems)
            {
                mrulist.DeleteUsage(lvi.SubItems[1].Text);
                lvi.Remove();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}